package com.stablesoft.gamestat;

import com.stablesoft.gamestat.R;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class FirstRunActivity extends Activity {

	private EditText wotLogin;
	private EditText mwoLogin;
	private Button btnSave;
	private TextView txtWarning;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_first_run);
		
		wotLogin = (EditText) findViewById(R.id.wotLogin);
		mwoLogin = (EditText) findViewById(R.id.mwoLogin);
		btnSave = (Button) findViewById(R.id.btnSave);
		txtWarning = (TextView) findViewById(R.id.warningMessage);
		
		updateSaveButton();
		
		wotLogin.addTextChangedListener(mLoginChangeListener);
		mwoLogin.addTextChangedListener(mLoginChangeListener);
		mwoLogin.setEnabled(false);
		
		btnSave.setOnClickListener(mSaveBtnClick);
	}

	private void updateSaveButton() {
		if (wotLogin.getText().length() > 0 || mwoLogin.getText().length() > 0) {
			btnSave.setEnabled(true);
			txtWarning.setVisibility(View.GONE);
		}
		else {
			btnSave.setEnabled(false);
			txtWarning.setVisibility(View.VISIBLE);
		}
	}
	private TextWatcher mLoginChangeListener = new TextWatcher() {
		public void onTextChanged(CharSequence s, int start, int before, int count) {
		}
		
		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {
		}
		
		@Override
		public void afterTextChanged(Editable s) {
			updateSaveButton();
		}
	};
	
	private View.OnClickListener mSaveBtnClick = new View.OnClickListener() {
		
		@Override
		public void onClick(View v) {
			SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
            SharedPreferences.Editor editor = settings.edit();
            editor.putString(SettingsActivity.WOT_LOGIN_KEY, wotLogin.getText().toString());
            editor.putString(SettingsActivity.MWO_LOGIN_KEY, mwoLogin.getText().toString());
            editor.commit();
			
            Intent intent = new Intent();
            intent.putExtra("ok", true);
            setResult(MainActivity.FIRST_RUN_CODE, intent);
            finish();
		}
	};
}
